package com.javen.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javen.entitys.User;
import com.javen.service.IUserService;
import com.javen.vo.LoginVO;

/**
 *   
 */
@Controller
@RequestMapping("/user")
// /user/**
public class UserController {
	private static Logger log = LoggerFactory.getLogger(UserController.class);

	@Resource
	private IUserService userService;
	@Autowired
	private HttpServletRequest request;
    /**
           * 登录验证
     * @param model
     * @return
     */
//	@RequestMapping(value = "/login")
//	public String login(Model model, String username, String password) {
//		log.info("username:" + username);
//		log.info("password:" + password);
//		
//		model.addAttribute("username", username);
//		return "login";
//	}
	
	/**
	 * 登陆接口
	 * @param model
	 * @param loginVO
	 * @return
	 */
	@RequestMapping(value = "/login")
	public String login(Model model, LoginVO loginVO) {
		log.info("username:" + loginVO.getUsername());
		log.info("password:" + loginVO.getPassword());
		
		model.addAttribute("loginVO", loginVO.toString());
		return "login";
	}

	/**
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	// /user/test?id=1
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test(HttpServletRequest request, Model model) {
		int userId = 0;
		try {
			userId = Integer.parseInt(request.getParameter("id"));
		} catch (Exception e) {

			model.addAttribute("message", "请传输正确的id类型");
			return "error";
		}

		System.out.println("userId:" + userId);

		//
		User user = userService.getUserById(userId);

		log.debug(user.toString());
		model.addAttribute("user", user);
		return "index";
	}

	/*
	 * 插入样例
	 */
	@RequestMapping(value = "/addUser", method = RequestMethod.GET)
	public String add(HttpServletRequest request, Model model) {
		User user = new User();
		user.setAge(12);
		user.setPassword("123456");
		user.setUserName("chenbobo");
		userService.insertSelective(user);
		model.addAttribute("user", user);
		return "index";
	}

	@RequestMapping(value = "/updateUser", method = RequestMethod.GET)
	public String update(Model model) {
		int userId = Integer.parseInt(request.getParameter("id"));
		System.out.println("userId:" + userId);

		User user = new User();
		user.setAge(Integer.parseInt(request.getParameter("age")));
		user.setPassword("ddf456");
		user.setUserName("vvv");
		user.setId(userId);

		userService.updateByPrimaryKeySelective(user);
		model.addAttribute("user", user);
		return "index";
	}

	@RequestMapping(value = "/delUser", method = RequestMethod.GET)
	@ResponseBody
	public String del(HttpServletRequest request, Model model) {
		int userId = Integer.parseInt(request.getParameter("id"));
		System.out.println("userId:" + userId);

		int i = userService.deleteByPrimaryKey(userId);
		if (i > 0) {

			return "success";
		} else {

			return "failed";
		}

	}

	/*
	 * // /user/showUser?id=1
	 * 
	 * @RequestMapping(value="/showUser",method=RequestMethod.GET) public String
	 * toIndex(HttpServletRequest request,Model model){ int userId =
	 * Integer.parseInt(request.getParameter("id"));
	 * System.out.println("userId:"+userId); User user =
	 * this.userService.getUserById(userId); log.debug(user.toString());
	 * model.addAttribute("user", user); return "showUser"; }
	 * 
	 * // /user/showUser2?id=1
	 * 
	 * @RequestMapping(value="/showUser2",method=RequestMethod.GET) public String
	 * toIndex2(@RequestParam("id") String id,Model model){ int userId =
	 * Integer.parseInt(id); System.out.println("userId:"+userId); User user =
	 * this.userService.getUserById(userId); log.debug(user.toString());
	 * model.addAttribute("user", user); return "showUser"; }
	 * 
	 * 
	 * // /user/showUser3/{id}
	 * 
	 * @RequestMapping(value="/showUser3/{id}",method=RequestMethod.GET) public
	 * String toIndex3(@PathVariable("id")String id,Map<String, Object> model){ int
	 * userId = Integer.parseInt(id); System.out.println("userId:"+userId); User
	 * user = this.userService.getUserById(userId); log.debug(user.toString());
	 * model.put("user", user); return "showUser"; }
	 * 
	 * // /user/{id}
	 * 
	 * @RequestMapping(value="/showUser4/{id}",method=RequestMethod.GET)
	 * 
	 * @ResponseBody public User getUserInJson(@PathVariable String id,Map<String,
	 * Object> model){ int userId = Integer.parseInt(id);
	 * System.out.println("userId:"+userId); User user =
	 * this.userService.getUserById(userId); log.info(user.toString()); return user;
	 * }
	 * 
	 * // /user/{id}
	 * 
	 * @RequestMapping(value="/jsontype/{id}",method=RequestMethod.GET) public
	 * ResponseEntity<User> getUserInJson2(@PathVariable String id,Map<String,
	 * Object> model){ int userId = Integer.parseInt(id);
	 * System.out.println("userId:"+userId); User user =
	 * this.userService.getUserById(userId); log.info(user.toString()); return new
	 * ResponseEntity<User>(user,HttpStatus.OK); }
	 * 
	 * //文件上传、
	 * 
	 * @RequestMapping(value="/upload") public String showUploadPage(){ return
	 * "user_admin/file"; }
	 * 
	 * @RequestMapping(value="/doUpload",method=RequestMethod.POST) public String
	 * doUploadFile(@RequestParam("file")MultipartFile file) throws IOException{ if
	 * (!file.isEmpty()) { log.info("Process file:{}",file.getOriginalFilename()); }
	 * FileUtils.copyInputStreamToFile(file.getInputStream(), new
	 * File("E:\\",System.currentTimeMillis()+file.getOriginalFilename())); return
	 * "succes"; }
	 */
}